package mult 

import org.scalatest._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import chisel3._
import chiseltest._

import scala.util._

class SAMultSpec extends AnyFlatSpec with ChiselScalatestTester with Matchers{

  behavior of "Shift-Add Multiplier"

  it should s"multiply two operands and yield correct result" in {
    test(new ShiftAddMul(8)) {mult =>

      val rand = new Random()
      val A = rand.nextInt(1 << mult.bits)
      val B = rand.nextInt(1 << mult.bits)
      // println((A, B))

      // Start loading multiplier (operand) into move
      mult.in.load.poke(true)
      mult.in.A.poke(A.U)
      mult.in.B.poke(B.U)
      mult.out.P.expect(0.U)
      mult.clock.step()
      mult.out.M.expect(B.U)

      // Begin to do the math.
      mult.in.load.poke(false)
      for (i <- 0 until mult.bits) {
        mult.clock.step()
        mult.out.M.expect((B << (1 + i)).U)

        // check the correctness of addition for each step.
        // println((mult.out.P.peek().litValue, mult.out.c.peek().litValue))
      }
      mult.out.P.expect((B * A).U)
    } 
  }
}
